package dyn 
{
	/**
	 * ...
	 * @author Excel Excel
	 */
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import dyn.LazyLoad;
	
	public class TileFamily
	{
		public var bm:Vector.<BitmapData>;
		public var bn:Vector.<uint>;
		public var color:uint;
		public var llds:Vector.<LazyLoad>;
		public function rand(seed:uint):uint
		{
			return (seed * 43691 + 911) % 65537;
		}
			
			
		public function GetDirect(n:uint):BitmapData
		{
			var bd:BitmapData = null;
			if (n > bm.length) throw "Bad bitmap index!";
			bd = bm[n];
			if (bd.width == 0) throw "Bad map width";
			return bd;
		}
		
		public function GetTile(seed:uint):BitmapData
		{
			var tot:int;
			for (var i:int = 0; i < bn.length; i++)
			{
				tot += bn[i];
			}
			var q:int = rand(seed) % tot;
			for (var i:int = 0; i < bn.length; i++)
			{
				q -= bn[i];
				if (q < 0)
					return bm[i];
			}
			trace("This is ridiculous!");
			return null;
		}
		
		public function TileFamily(col:uint) 
		{
			bm = new Vector.<BitmapData>();
			bn = new Vector.<uint>();
			llds = new Vector.<LazyLoad>();
			color = col;
		}
		public function AddTile(s:String, i:int)
		{
			if (!i) i = 1;
		    llds.push(new LazyLoad(this, s, i));
		}
		
		public function get done () : Boolean
		{
			for (var i:int = 0; i < llds.length; i++)
			{
				if (!llds[i].done) return false;
			}
			return true;
		}
		
		public function get error () : Boolean
		{
			for (var i:int = 0; i < llds.length; i++)
			{
				if (llds[i].error) 
					return true;
			}
			return false;
		}
				
	}

}